package com.holy.sh.couviewholy.pic;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.holy.sh.couviewholy.R;

/**
 * Holy Email :victorhhl@163.com
 * Create by Holy on 2019/8/12
 * Description : 图片阴影
 * 知识点：1先取图片透明Alpha bitmap 设置模糊 然后偏移画布画原图
 * extractAlpha 取底部纯色图
 * setMaskFilter 设置纯色图高亮状态
 *
 */
public class ExtractAlphaPicView extends View {
    private Paint mPaint;
    Bitmap mBitmap;
    Bitmap mAlphaBmp;

    public ExtractAlphaPicView(Context context) {
        super(context);
        initOP();
    }


    public ExtractAlphaPicView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initOP();
    }

    public ExtractAlphaPicView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initOP();
    }

    private void initOP() {
        mPaint = new Paint();
        mPaint.setColor(Color.GRAY);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(2);
        mPaint.setAntiAlias(true);
        setLayerType(LAYER_TYPE_SOFTWARE,null);
        // mPaint.setStrokeMiter(90);//设置画笔倾斜度
        //mPaint.setPathEffect();//设置路径样式 CopmosePathEffect-》》合并路径类型用的
        // CornerPathEffect，DashPathEffect，DiscreteOathEffect，pathDashPathEffect,SumpathEffect
        //mPaint.setStrokeCap(); Cap.ROUND`SQUARE`BUTT
        //mPaint.setStrokeJoin(Paint.Join.BEVEL); //设置路径转角样式 MITER·ROUND·BEVEL
        //mPaint.setDither(true);//抗抖动，颜色丰富无法显示，过度显示马赛克相邻像素中间插入过渡值

        //Paint 字体相关
        //mPaint.setTextSize();
        mPaint.setFakeBoldText(false);//加粗
        mPaint.setStrikeThruText(false);//删除线
        mPaint.setUnderlineText(false);//下划线
        //mPaint.setTextAlign(false);//开始绘图点位置？
        //mPaint.setTextScaleX();//设置水平拉伸
        //mPaint.setTextSkewX();//设置水平斜度
        //mPaint.setTypeface();设置字体样式

        mBitmap= BitmapFactory.decodeResource(getResources(), R.mipmap.logo);
        mAlphaBmp  = mBitmap.extractAlpha();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width=200;
        int height=width*mAlphaBmp.getWidth()/mAlphaBmp.getHeight();
        mPaint.setMaskFilter(new BlurMaskFilter(10, BlurMaskFilter.Blur.NORMAL));
        canvas.drawBitmap(mAlphaBmp,null,new Rect(10,10,width,height),mPaint);
        canvas.translate(-5,-5);//画布平移
        mPaint.setMaskFilter(null);
        canvas.drawBitmap(mBitmap,null,new Rect(10,10,width,height),mPaint);
    }
}
